const express = require("express");
const cookieParser = require("cookie-parser")
const passport = require("passport")
let multer = require("multer")
const path = require("path")
var io = require('socket.io');
var logger = require('morgan');
const fs = require("fs")

const userRouter = require("./routes/user");
const blogRouter = require("./routes/blog");
const commentRouter = require("./routes/comment")
const recommentRouter = require("./routes/recomment")
const adviceRouter = require("./routes/advice")
const chatRouter = require("./routes/chat")
const prodRouter = require("./routes/prod")
const chatRoomRouter = require("./routes/chatRoom")
const traceRouter = require("./routes/trace")
const collectRouter = require("./routes/collect")
const historyRouter = require("./routes/history")
const navigationRouter = require("./routes/navigation")
const pdfRouter = require("./routes/PDF")
const musicRouter = require("./routes/music")
const {
  All,
  delOp,
  friendOp
} = require("./config/kuayu")
const {
  upload,
  uploadMp4,
  uploadPDF,
  uploadMusic
} = require("./config/upload")
const {
  Avatar
} = require("./control/user")
const { PDF } = require("./control/PDF")
const {
  Prod,
  Poster
} = require("./control/prod")
const {Music} = require("./control/music")
// const uploadRouter = require("./routes/upload")

const app = express()

// const ENV = process.env.NODE_ENV
// if(ENV=="dev"){
//   app.use(logger('dev',{
//     stream:process.stdout
//   }));
// }else{
//   //线上环境
//   const logFileName = path.join(__dirname,'logs','access.log')
//   const writeStream = fs.createWriteStream(logFileName,{flags:'a'})
//   app.use(logger('combined',{
//     stream:writeStream
//   }));
// }

app.use(express.json())
app.use(express.urlencoded({
  extended: false
}))
app.use(cookieParser())
//初始化passport
app.use(passport.initialize());
//设置跨域访问
app.use(express.static(path.join(__dirname, 'public')));

app.use("/", function (req, res, next) {
  All(req, res, next)
})

//设置跨域访问
app.use("/api/blog/add", function (req, res, next) {
  delOp(req, res, next)
})
//设置跨域访问
app.use("/api/blog/del", function (req, res, next) {
  delOp(req, res, next)

})
//设置跨域访问
app.use("/api/blog/update", function (req, res, next) {
  delOp(req, res, next)
})
app.use("/api/prod/del", function (req, res, next) {

  friendOp(req, res, next)
})
app.use("/api/navigation/add", function (req, res, next) {
  delOp(req, res, next)

})
app.use("/api/navigation/del", function (req, res, next) {
  delOp(req, res, next)

})
app.use("/api/navigation/update", function (req, res, next) {
  delOp(req, res, next)

})
// app.use("/api/pdf",function(req,res,next){
//   delOp(req, res, next)

// })
// app.use("/api/upload/upMp4", function (req, res, next) {
//     console.log(req.body);

//   friendOp(req, res, next)
// })


//jwt身份验证
require("./util/jwt/passport-jwt")(passport)


app.use("/api/user", userRouter)
app.use("/api/blog", blogRouter)
app.use("/api/comment", commentRouter)
app.use("/api/recomment", recommentRouter)
app.use("/api/advice", adviceRouter)
app.use("/api/chat", chatRouter)
app.use("/api/prod", prodRouter)
app.use("/api/chatRoom", chatRoomRouter)
app.use("/api/trace", traceRouter)
app.use("/api/collect", collectRouter)
app.use("/api/history", historyRouter)
app.use("/api/navigation", navigationRouter)
app.use("/api/pdf", pdfRouter)
app.use("/api/music", musicRouter)
app.get("/api/avatar", async function (req, res) {
  res.sendFile(path.join(__dirname, './public/images/avatar.jpg'))
})
//图片存放位置
let uploadImg = multer({
  dest: './public/images'
})
let uploadp4 = multer({
  dest: "./public/Mp4"
})
let uploadpdf = multer({
  dest: "./public/PDF"
})
let uploadmusic = multer({
  dest:"./public/music"
})
app.post("/api/upload/music", uploadmusic.array('musicfile',5), async function (req,res){
  uploadMusic(req,res)
})
app.post("/api/upload/PDF", uploadpdf.array('fileList', 5), async function (req, res) {
  uploadPDF(req, res)
})
//上传头像 imgfile 前端传过来的文件的名字
app.post('/api/upload/up', uploadImg.single('imgfile'), async function (req, res) {
  upload(req, res)

})

app.post('/api/upload/upMp4', uploadp4.array('mp4file', 2), async function (req, res) {

  uploadMp4(req, res)
})


app.get("/api/upload/showImg", async function (req, res) {
  let data = await Avatar(req, res)
  if (data.length > 0) {
    res.sendFile(path.join(__dirname, req.query.avatar))
  } else {
    res.send("失败")
  }
})

app.get("/api/upload/music", async function (req, res) {
  let data = await Music(req, res)
  if (data.length > 0) {
    res.sendFile(path.join(__dirname, req.query.music))
  } else {
    res.send("失败")
  }
})


app.get("/api/upload/PDF", async function (req, res) {
  // let data = await PDF(req, res)
  // if (data.length > 0) {
  console.log(path.join(__dirname, req.query.pdf));
  res.sendFile(path.join(__dirname,req.query.pdf),function (e) {
    console.log(e)
  })
  // res.send(path.join(__dirname, req.query.pdf))
  // } else {
  // res.send("失败")
  // }
  // res.set({
  //   'Content-Type': 'application/octet-stream',
  //   // 'Content-Length': stat.size
  //   "Content-Disposition":'attachment'
  // });
  // fs.readFile(path.join(__dirname, req.query.pdf),function(err,data){
  //   console.log(data)
  //   res.send(data)
  // });
  

  // var readStream = fs.createReadStream(path.join(__dirname, req.query.pdf));
  // return readStream.pipe(res);
})
app.get("/api/upload/showMp4", async function (req, res) {
  if (req.query.prod) {
    let data = await Prod(req, res)
    if (data.length > 0) {
      res.sendFile(path.join(__dirname, './' + req.query.prod))
    } else {
      res.send("失败")

    }
  } else {
    let data = await Poster(req, res)
    if (data.length > 0) {
      res.sendFile(path.join(__dirname, req.query.poster))
    } else {
      res.send("失败")

    }
  }

})

app.use(function (err, req, res, next) {
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'dev' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.send('error');
})


module.exports = app;